package com.xj.api.bussiness.wxPay.web;

import com.alibaba.fastjson.JSONObject;
import com.thoughtworks.xstream.XStream;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.wxPay.common.Configure;
import com.xj.api.bussiness.wxPay.common.Signature;
import com.xj.api.bussiness.wxPay.entity.OrderInfo;
import com.xj.api.bussiness.wxPay.entity.OrderReturnInfo;
import com.xj.api.bussiness.wxPay.entity.SignInfo;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.util.HttpRequest;
import com.xj.common.base.common.util.RandomStringGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wxPay")
public class WxPayController extends BaseController {

//    @Autowired
//    private

    @PostMapping("/placeAnOrder")
    @ApiOperation(value = "  下单", httpMethod = "POST", notes = "  下单")
    public AbstractBean placeAnOrder(OrderInfo order)throws ServletException, IOException {
        order.setAppid(Configure.getAppID());
        order.setMch_id(Configure.getMch_id());
        order.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));//随机字符串，长度要求在32位以内。
        //order.setBody("dfdfdf");通过小程序来描述商品
        order.setOut_trade_no(RandomStringGenerator.getRandomStringByLength(32)); //商户订单号
        //order.setTotal_fee(10); 多少大洋-以分为单位
        order.setSpbill_create_ip("119.23.111.186");
        order.setNotify_url("https://www.gcjxglzx.com/wxPay/payResult");//通知地址
        order.setTrade_type("JSAPI");
        //order.setOpenid(openid);//用户的id
        order.setSign_type("MD5");
        try {
            //签名算法  order 要参与签名的数据对象
            String sign = Signature.getSign(order);
            order.setSign(sign);//获得MD5的签名字符串
            String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order);
            XStream xStream = new XStream();
            xStream.alias("xml", OrderReturnInfo.class);
            OrderReturnInfo returnInfo = (OrderReturnInfo)xStream.fromXML(result);
            Map<String,String> map = new HashMap<String, String>();
            map.put("prepay_id", returnInfo.getPrepay_id());
            return json(map);
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    @PostMapping("/autoGraph")
    @ApiOperation(value = "  签名", httpMethod = "POST", notes = "  签名")
    public AbstractBean autoGraph(@RequestParam(required=true)String repay_id){
        try {
            SignInfo signInfo = new SignInfo();
            signInfo.setAppId(Configure.getAppID());
            long time = System.currentTimeMillis()/1000;
            signInfo.setTimeStamp(String.valueOf(time));
            signInfo.setNonceStr(RandomStringGenerator.getRandomStringByLength(32));
            signInfo.setRepay_id("prepay_id="+repay_id);
            signInfo.setSignType("MD5");
            //生成签名
            String sign = Signature.getSign(signInfo);
            Map<String,String> map = new HashMap<String, String>();
            map.put("timeStamp", signInfo.getTimeStamp());
            map.put("nonceStr", signInfo.getNonceStr());
            map.put("package", signInfo.getRepay_id());
            map.put("signType", signInfo.getSignType());
            map.put("paySign", sign);
            return json(map);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           return error();
        }
    }

    /** 暂时不用
    @GetMapping("/payResult")
    @ApiOperation(value = "  签名", httpMethod = "GET", notes = "  签名")
    public void payResult(){
        System.out.println("这里是调用的通知地址");
    }
    */
}
